package com.fhzn.boss.web.converter;

import com.fhzn.boss.context.RequestContext;
import com.fhzn.boss.entity.Label;
import com.fhzn.boss.entity.LabelExtension;
import com.fhzn.boss.web.request.LabelsOpRequest;
import com.fhzn.boss.web.vo.LabelExportVO;
import com.fhzn.boss.web.vo.LabelVO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/3/25
 */
public interface LabelConverter {

    /**
     * 标签转标签VO
     *
     * @param label 标签
     * @return vo
     */
    static LabelVO fromLabel(Label label) {
        LabelVO labelVO = new LabelVO();
        if (label != null) {
            labelVO.setId(label.getId());
            labelVO.setParentId(label.getParentId());
            labelVO.setType(label.getType());
            labelVO.setCode(label.getCode());
            labelVO.setName(label.getName());
            labelVO.setAppCode(label.getAppCode());
            labelVO.setComment(label.getComment());
            labelVO.setExtension(label.getExtension());
            labelVO.setCreator(label.getCreator());
            labelVO.setModifier(label.getModifier());
            labelVO.setCreatedTime(label.getCreatedTime());
            labelVO.setUpdatedTime(label.getUpdatedTime());
        }
        return labelVO;
    }

    /**
     * 标签转标签
     *
     * @param request 标签请求
     * @return 标签
     */
    static Label fromOpLabel(LabelsOpRequest request) {
        Label label = new Label();
        if (request != null) {
            if (request.getId() != null) {
                label.setId(request.getId());
            }
            if (request.getParentId() != null) {
                label.setParentId(request.getParentId());
            } else {
                label.setParentId(0);
            }
            if (request.getType() != null) {
                label.setType(request.getType());
            } else {
                label.setType(1);
            }
            if (request.getCode() != null && !request.getCode().isEmpty()) {
                label.setCode(request.getCode());
            } else {
                label.setCode("");
            }
            label.setName(request.getName());
            label.setAppCode(request.getAppCode());
            label.setComment(request.getComment());
            if (Objects.isNull(request.getExtension())) {
                label.setExtension(new LabelExtension());
            } else {
                label.setExtension(request.getExtension());
            }
        }
        return label;
    }

    /**
     * 标签VO转标签
     *
     * @param labelVO 标签VO
     * @return 标签
     */
    static Label fromLabelVO(LabelVO labelVO) {
        Label label = new Label();
        if (labelVO != null) {
            label.setId(labelVO.getId());
            label.setParentId(labelVO.getParentId());
            label.setType(0);
            label.setCode(labelVO.getCode());
            label.setName(labelVO.getName());
            label.setAppCode(labelVO.getAppCode());
            label.setCreator(RequestContext.getRequestData().getHandler());
            label.setModifier(RequestContext.getRequestData().getHandler());
            label.setCreatedTime(new Date());
            label.setUpdatedTime(new Date());
        }
        return label;
    }

    /**
     * 标签列表转标签VO列表
     *
     * @param labels 标签列表
     * @return 标签VO列表
     */
    static List<LabelExportVO> fromLabelList(List<Label> labels) {
        if (CollectionUtils.isEmpty(labels)) {
            return Lists.newArrayList();
        }
        return labels.stream().map(LabelConverter::fromExportLabel).collect(Collectors.toList());
    }

    /**
     * 标签转标签VO
     *
     * @param label 标签
     * @return vo
     */
    static LabelExportVO fromExportLabel(Label label) {
        LabelExportVO labelExportVO = new LabelExportVO();
        if (label != null) {
            labelExportVO.setId(label.getId());
            labelExportVO.setType(label.getType());
            labelExportVO.setParentId(label.getParentId());
            labelExportVO.setCode(label.getCode());
            labelExportVO.setName(label.getName());
            labelExportVO.setComment(label.getComment());
            labelExportVO.setExtension(label.getExtension());
            labelExportVO.setCreatedTime(label.getCreatedTime());
            labelExportVO.setUpdatedTime(label.getUpdatedTime());
        }
        return labelExportVO;
    }

}
